package org.jeecg.modules.tt_question_collect.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import kong.unirest.JsonNode;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.tt_question_collect.entity.TtPlanConfirm;
import org.jeecg.modules.tt_question_collect.entity.TtQuestionCollect;
import org.jeecg.modules.tt_question_collect.entity.TtQuestionDealRecord;
import org.jeecg.modules.tt_question_collect.entity.tspmodel;
import org.jeecg.modules.tt_question_collect.mapper.TtQuestionCollectMapper;
import org.jeecg.modules.tt_question_collect.service.ITtQuestionCollectService;
import org.jeecg.modules.tt_question_collect.util.MsgTypeEnum;
import org.jeecg.modules.tt_question_collect.util.PushSysMsgUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @Description: 问题收集表
 * @Author: jeecg-boot
 * @Date: 2021-01-29
 * @Version: V1.0
 */
@Service
public class TtQuestionCollectServiceImpl extends ServiceImpl<TtQuestionCollectMapper, TtQuestionCollect> implements ITtQuestionCollectService {
    @Resource
    private TtQuestionCollectMapper TtQuestionCollectMapper;

    @Autowired
    private PushSysMsgUtil pushSysMsgUtil;

    //处理
    @Override
    public Page<TtQuestionCollect> queryquestioncl(Page<TtQuestionCollect> page, TtQuestionCollect ttQuestionCollect) {

        if ("未分配".equals(ttQuestionCollect.getFashbj())){
            ttQuestionCollect.setFatcuserid("6");
            ttQuestionCollect.setFashbj(null);
        }
        return page.setRecords(TtQuestionCollectMapper.querySysCementListByUserId(page, ttQuestionCollect));
    }

    //审批
    @Override
    public Page<tspmodel> queryquestionsp(Page<tspmodel> page, tspmodel tspmodel) {
        return page.setRecords(TtQuestionCollectMapper.querysplist(page, tspmodel));
    }

    @Override
    public TtQuestionDealRecord querybyquestioncode(String questioncode) {
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();

        return (TtQuestionCollectMapper.querybyquestioncode(questioncode, user.getId()));
    }

    @Override
    public List<Map<String, Object>> querydetp(String id) {
        return TtQuestionCollectMapper.querydetplist(id);
    }

    @Override
    public String querysplcid(String bmid,String lclx) {
        return TtQuestionCollectMapper.queryidBybmid(bmid,lclx);
    }


    @Override
    public void setJiraIdOrKey(String id, JsonNode body) {
        JSONObject jsonObject = JSONObject.parseObject(body.toString());
        String jiraId = jsonObject.getString("id");
        String jiraKey = jsonObject.getString("key");
        TtQuestionCollectMapper.setJiraIdOrKey(id, jiraId, jiraKey);
    }

    @Override
    public void setStatusByJiraIdOrKey(String id, JsonNode body) {
        JSONObject jsonObject = JSONObject.parseObject(body.toString());
        JSONObject fields = jsonObject.getJSONObject("fields");
        JSONObject status = fields.getJSONObject("status");
        String name = status.getString("name");
        int count = TtQuestionCollectMapper.setJiraStatu(id, name);
        if (count > 0 && "发布完成".equals(name)) {
            try {
                pushSysMsgUtil.sendMessage(MsgTypeEnum.A06.getMsgTpeCode(), id);
            }
            catch (Exception e) {
                log.error(e.getMessage(), e);
            }
        }
    }

    //问题列表
    @Override
    public Page<TtQuestionCollect> queryquestionlist(Page<TtQuestionCollect> page, TtQuestionCollect ttQuestionCollect) {
    	if ("待办".equals(ttQuestionCollect.getAuditstatus()) || "处理中".equals(ttQuestionCollect.getAuditstatus())
    			|| "测试环境更新".equals(ttQuestionCollect.getAuditstatus()) || "代码完成".equals(ttQuestionCollect.getAuditstatus())
    			|| "测试完成".equals(ttQuestionCollect.getAuditstatus())
    			|| "待发布".equals(ttQuestionCollect.getAuditstatus())
    			|| "发布完成".equals(ttQuestionCollect.getAuditstatus())
    			|| "完成".equals(ttQuestionCollect.getAuditstatus())) {
    		//通过
			ttQuestionCollect.setDealstatus(ttQuestionCollect.getAuditstatus());
			ttQuestionCollect.setAuditstatus("2");
		}else if ("开发方案出具中".equals(ttQuestionCollect.getAuditstatus()) || "方案已确认".equals(ttQuestionCollect.getAuditstatus())){
                  ttQuestionCollect.setBy1(ttQuestionCollect.getAuditstatus());
                  ttQuestionCollect.setAuditstatus("2");
        }

		 return page.setRecords(TtQuestionCollectMapper.queryquestionList(page, ttQuestionCollect));

    }

	@Override
	public void updatescbjById(String id) {
		// TODO Auto-generated method stub
		TtQuestionCollectMapper.updatescbjbyid(id);

	}

    @Override
    public String cxxl() {
        return TtQuestionCollectMapper.cxxl();
    }

    @Override
    public List<Map<String,Object>> getuserrole(String userid) {
      List<Map<String,Object>> rolelist = TtQuestionCollectMapper.getrolelist(userid);

        return rolelist;
    }

    @Override
    public List<Map<String, Object>> queryupdatejira() {
        return (TtQuestionCollectMapper.queryupdatejiralist());
    }

    @Override
    public List<Map<String, Object>> getjiratblist() {
            return (TtQuestionCollectMapper.getjiratblist());
        }

    @Override
    public String queryuserdspcount(String id) {
        return TtQuestionCollectMapper.queryuserdspcount(id);
    }

}




